home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python152_Src.lha / Python152_Source / Amiga / _write.c < prev    next >
C/C++ Source or Header  |  1998-12-25  |  2KB  |  117 lines

  1. RCS_ID_C="$Id: _write.c,v 4.1 1994/09/29 23:09:02 jraja Exp $";
  2. /*
  3.  *      _write.c - write() for both files and sockets (SAS/C)
  4.  *
  5.  *      Copyright © 1994 AmiTCP/IP Group, 
  6.  *                       Network Solutions Development Inc.
  7.  *                       All rights reserved.
  8.  */
  9.  
  10. #include <ios1.h>
  11. #include <fcntl.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <dos.h>
  15. #include <string.h>
  16. #include <errno.h>
  17. #include <dos/dos.h>
  18. #include <proto/dos.h>
  19.  
  20. #include <bsdsocket.h>
  21. #include "libcheck.h"
  22.  
  23. //extern int __io2errno(long);
  24.  
  25. int
  26. __write(int fd, const void *buffer, unsigned int length)
  27. {
  28.   struct UFB *ufb;
  29.   int         count, totcount;
  30.   char       *ptr;
  31.  
  32.   /*
  33.    * Check for the break signals
  34.    */
  35.   __chkabort();
  36.   /*
  37.    * find the ufb *
  38.    */
  39.   if ((ufb = __chkufb(fd)) == NULL) {
  40.     errno = EINVAL;
  41.     return -1;
  42.   }
  43.   /*
  44.    * Check if write is allowed
  45.    */
  46.   if (!(ufb->ufbflg & UFB_WA)) {
  47.     _OSERR = ERROR_WRITE_PROTECTED;
  48.     errno = EIO;
  49.     return -1;
  50.   }
  51.  
  52.   /*
  53.    * Seek to end of the file if necessary
  54.    */
  55.   if (ufb->ufbflg & UFB_APP)
  56.     __lseek(fd, 0, 2);
  57.  
  58.   /* check if socket, then this function needs bsdsocket.library  -- I.J. */
  59.   if (ufb->ufbflg & UFB_SOCK)
  60.     if(!checksocketlib()) return -1;
  61.  
  62.   /*
  63.    * Check if translation is not needed
  64.    */
  65.   if (!(ufb->ufbflg & UFB_XLAT) ||
  66.       (ptr = memchr(buffer, 0x0A, length)) == NULL) {
  67.     if (ufb->ufbflg & UFB_SOCK) {
  68.       if ((count = send(fd, (char *)buffer, length, 0)) < 0)
  69.     return -1;
  70.     }
  71.     else {
  72.       if ((count = Write(ufb->ufbfh, (void *)buffer, length)) == -1)
  73.     goto osfail;
  74.     }
  75.     return count;
  76.   }
  77.  
  78.   totcount = length;
  79.  
  80.   /*
  81.    * Translate, ie., append CR before each LF
  82.    */
  83.   do {
  84.     count = ptr - (char *)buffer;
  85.     if (ufb->ufbflg & UFB_SOCK) {
  86.       if (send(fd, (char *)buffer, count, 0) < 0)
  87.     return -1;
  88.       if (send(fd, "\015"/* CR */, 1, 0) < 0)
  89.     return -1;
  90.     }
  91.     else {
  92.       if (Write(ufb->ufbfh, (void *)buffer, count) == -1)
  93.     goto osfail;
  94.       if (Write(ufb->ufbfh, "\015"/* CR */, 1) == -1)
  95.     goto osfail;
  96.     }
  97.     length -= count;
  98.     
  99.     buffer = ptr;
  100.   } while ((ptr = memchr((char *)buffer + 1, 0x0A, length)) != NULL);
  101.   
  102.   if (ufb->ufbflg & UFB_SOCK) {
  103.     if ((count = send(fd, (char *)buffer, length, 0)) < 0)
  104.       return -1;
  105.   }
  106.   else {
  107.     if (Write(ufb->ufbfh, (void *)buffer, length) == -1)
  108.       goto osfail;
  109.   }
  110.  
  111.   return totcount;
  112.  
  113. osfail:
  114.   errno = __io2errno(_OSERR = IoErr());
  115.   return -1;
  116. }
  117.